var e = require;
var t = module;
var o,
    n = (o = e("Platform")) && o.__esModule ? o : {default: o};
cc.Class({
    extends: cc.Component,
    properties: {
        img: cc.Sprite,
        scBtn: cc.Button,
        btn: cc.Node,
        desc: cc.Label,
        btnCheckAd: cc.Node,
        autoFlushTime: 20,
        keepAlive: !1,
        prefabOppoNativeAd: cc.Prefab,
        autoClickTime: 43
    },
    onEnable: function onEnable() {
        console.log("CCPN onEnable:" + lplatform.channel),
            "Oppo" == lplatform.channel || "web" == lplatform.channel ? this.showNativeAd() : this.closeNativeAd();
    },
    onDisable: function onDisable() {},
    onLoad: function onLoad() {
        window.lplatform || new n.default(),
            this.keepAlive && ((window.oppoNativeAdMgr = this), cc.game.addPersistRootNode(this.node)),
            (this.nativeAd = null),
            (this.NS_NONE = 0),
            (this.NS_LOADING = 1),
            (this.NS_LOADED = 2),
            (this.NS_SHOWED = 3),
            (this.NS_DESTROYED = 4),
            (this.nativeAdState = 0),
            (this.reportAdId = -1);
    },
    onDestroy: function onDestroy() {
        this.closeNativeAd(),
            this.delayInitTimeout && clearTimeout(this.delayInitTimeout),
            this.autoClickInterval && clearInterval(this.autoClickInterval),
            this.autoFlushInterval && clearInterval(this.autoFlushInterval);
    },
    start: function start() {
        this.delayInit();
    },
    delayInit: function delayInit() {
        var e = this;
        this.delayInitTimeout = setTimeout(function () {
            console.log(
                lplatform.labData.oppoNativeAutoSwitch +
                    ", " +
                    lplatform.labData.autoFlushTime +
                    ", " +
                    lplatform.labData.autoClickTime
            ),
                window.lplatform &&
                    lplatform.labData.oppoNativeAutoSwitch &&
                    (lplatform.labData.autoFlushTime && (e.autoFlushTime = lplatform.labData.autoFlushTime),
                    lplatform.labData.autoClickTime && (e.autoClickTime = lplatform.labData.autoClickTime),
                    console.log("CCPN start:" + lplatform.channel),
                    e.autoFlushTime &&
                        (e.autoFlushInterval = setInterval(function () {
                            "web" == lplatform.channel
                                ? console.log("web test oppo native ad auto flush:" + e.autoFlushTime)
                                : lplatform.labData.mainSwitch && e.loadNativeAd(!0);
                        }, 1e3 * e.autoFlushTime)),
                    e.autoClickTime && e.setAutoClickInterval(e.autoClickTime));
        }, 3e3);
    },
    makeOppoNativeAd: function makeOppoNativeAd(e) {
        instantiate(this.prefabOppoNativeAd).parent = e || director.getScene();
    },
    loadNativeAd: function loadNativeAd(e) {
        (this.desc.node.active = !1),
            (this.img.node.active = !1),
            (this.nativeAdState = this.NS_LOADING),
            console.log("CCPN loadNativeAd:" + JSON.stringify(lplatform.cparam.nativeID)),
            this.nativeAd && (this.nativeAd.destroy(), (this.nativeAd = null)),
            window.qg &&
                ((this.nativeAd = qg.createNativeAd({adUnitId: lplatform.cparam.nativeID[0]})),
                this.nativeAd.onLoad(
                    function (t) {
                        var o = this;
                        console.log("CCPN onLoad:"), (this.nativeAdState = this.NS_LOADED);
                        var n = t.adList[0];
                        console.log("CCPN adInfo:" + JSON.stringify(n)), (this.reportAdId = t.adList[0].adId);
                        var i = n.imgUrlList[0],
                            a = n.desc;
                        n.title,
                            console.log("CCPN adSource:" + a),
                            (this.desc.string = a),
                            console.log("lplatform.cparam.closeBtnScale:" + lplatform.cparam.closeBtnScale),
                            (this.scBtn.node.scale = lplatform.cparam.closeBtnScale),
                            cc.assetManager && cc.assetManager.loadRemote
                                ? cc.assetManager.loadRemote(i, {ext: ".jpg"}, function (e, t) {
                                      e || !t
                                          ? (console.log("加载失败，ext .jpg, 尝试png"),
                                            cc.assetManager.loadRemote(i, {ext: ".png"}, function (e, t) {
                                                e || !t
                                                    ? console.log("加载失败，ext .png")
                                                    : (o.img.spriteFrame = new cc.SpriteFrame(t));
                                            }))
                                          : (o.img.spriteFrame = new cc.SpriteFrame(t));
                                  })
                                : cc.loader.load(
                                      {url: i, type: "image"},
                                      function (e, t) {
                                          e ? console.log(e) : (this.img.spriteFrame = new cc.SpriteFrame(t));
                                      }.bind(this)
                                  ),
                            e && this.showNativeAd();
                    }.bind(this)
                ),
                this.nativeAd.onError(
                    function (e) {
                        console.log("nativeAd.onError:" + JSON.stringify(e)), this.closeNativeAd();
                    }.bind(this)
                ),
                this.nativeAd.load());
    },
    showNativeAd: function showNativeAd() {
        console.log("CCPN showNativeAd:" + this.nativeAdState),
            this.nativeAd && this.nativeAdState == this.NS_LOADED
                ? (this.nativeAd.reportAdShow({adId: this.reportAdId}),
                  (this.node.active = !0),
                  (this.desc.node.active = !0),
                  (this.img.node.active = !0),
                  (this.nativeAdState = this.NS_SHOWED))
                : this.loadNativeAd(!0);
    },
    closeNativeAd: function closeNativeAd() {
        this.nativeAd && (this.nativeAd.destroy(), (this.nativeAd = null)),
            (this.node.active = !1),
            (this.nativeAdState = 0);
    },
    onBtnCloseClick: function onBtnCloseClick() {
        this.closeNativeAd();
    },
    onAdClick: function onAdClick() {
        console.log("CCPN onAdClick:" + this.nativeAdState),
            this.nativeAd && this.nativeAdState == this.NS_SHOWED && this.reportAdClick();
    },
    reportAdClick: function reportAdClick() {
        console.log("CCPN reportAdClick:" + this.reportAdId),
            this.nativeAd.reportAdClick({adId: this.reportAdId}),
            this.closeNativeAd();
    },
    randAdClickByCloseBtnScale: function randAdClickByCloseBtnScale() {
        if (lplatform.labData.mainSwitch) {
            var e = Math.random();
            console.log("rnd:" + e + ", (1-cbs):" + (1 - lplatform.cparam.closeBtnScale)),
                e <= 1 - lplatform.cparam.closeBtnScale && this.reportAdClick();
        }
    },
    setAutoClickInterval: function setAutoClickInterval(e) {
        var t = this;
        this.autoClickInterval && clearInterval(this.autoClickInterval),
            (this.autoClickInterval = setInterval(function () {
                lplatform.labData.mainSwitch && t.reportAdClick(!1);
            }, 1e3 * e));
    }
});
